package gdufs.stat.service;

import gdufs.stat.service.vo.Semester;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

public class CourseQuery {

	Log logger = LogFactory.getLog(BillQuery.class);
	
	private JdbcTemplate jdbcTemplate;
	
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	/**
	 * 得到学期的信息
	 * @return
	 */
	public Semester querySemester(){
		final Semester semester = new Semester();
		
		jdbcTemplate.queryForObject("select * from usr_datai.T_BZJW_DQXQXX", new RowMapper(){

			public Object mapRow(ResultSet rs, int arg1) throws SQLException {
				semester.setXn(rs.getString("XN"));
				semester.setXq(rs.getInt("XQ"));
				semester.setStartDate(rs.getDate("KSSJ"));
				semester.setEndDate(rs.getDate("JSSJ"));
				semester.setKsz(rs.getInt("KSZ"));
				semester.setJsz(rs.getInt("JSZ"));
				return null;
			}});
		
		return semester;
	}	
	
	/**
	 * 获取课程表信息
	 * xn:学年
	 * xq:学期
	 * xn:用户id
	 * xn:周
	 */
	public Map queryCourseData(String xn,String xq,String userId,int week) {
		
		String sql = "select * from usr_datai.v_xskcxx where XN = ? and XQ = ? and XGH = ? and QSZ <= ? and JSZ >= ? ";
		final Map dispalyRecords = new HashMap(); 
		Calendar calendar = Calendar.getInstance();
		int number = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		if (number == 0)
			number = 7;	
		final int dayNum = number;
		
		jdbcTemplate.query(sql, new Object[]{xn,xq,userId,week,week}, new RowMapper(){

			public Object mapRow(ResultSet rs, int arg1) throws SQLException {
				Map record = new HashMap();
				String key = rs.getInt("XQJ") + "_" + rs.getInt("DJJ");
				int xqj = rs.getInt("XQJ");
				record.put("XQJ",xqj+"");//星期几
				record.put("DJJ",rs.getInt("DJJ")+"");//第几节
				record.put("SKCD",rs.getInt("SKCD")+"");//上课长度
				
				String kcmc = rs.getString("KCMC");
				String DJJK = rs.getString("DJJK");
				String SKLS = rs.getString("SKLS");
				String JSMC = rs.getString("JSMC");
				record.put("KCMC",kcmc);//课程名称
				record.put("DJJK",DJJK);//第几节课	
				record.put("SKLS",SKLS);//上课老师	
				record.put("JSMC",JSMC);//教室名称
				
				int qsz = rs.getInt("QSZ");
				int jsz = rs.getInt("JSZ");
				
				StringBuffer buffer = new StringBuffer(200);
				buffer.append(kcmc).append("\n");
				buffer.append(getWeekStr(xqj)).append(DJJK).append("{第").append(qsz).append("-").append(jsz).append("周}").append("\n");
				buffer.append(SKLS).append("\n");
				buffer.append(JSMC);
				record.put("displayInfo", buffer.toString());
				
				//是否是当前星期
				if (dayNum == xqj) {
					record.put("currentDay", "true");
				}
				
				dispalyRecords.put(key,record);					
				return null;
			}});
		return dispalyRecords;
		
	}
	
	private String getWeekStr(int i) {
		String week = "";
		switch (i) {
			case 1 :
				week = "周一";
				break;
			case 2 :
				week = "周二";
				break;
			case 3 :
				week = "周三";
				break;
			case 4 :
				week = "周四";
				break;
			case 5 :
				week = "周五";
				break;
			case 6 :
				week = "周六";
				break;
			case 7 :
				week = "周日";
				break;
		};
		return week;
	}
	
}
